In [ ]:
# Computations
import pandas as pd
import numpy as np

# sklearn
from sklearn.metrics import classification_report, accuracy_score, f1_score, precision_score, recall_score
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split, GridSearchCV, RandomizedSearchCV, cross_val_score

# KMeans
from sklearn.cluster import KMeans

# preprocessing
from sklearn.preprocessing import StandardScaler
from sklearn.impute import SimpleImputer


# keras
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation
from keras.optimizers import SGD, Adagrad
from keras.utils.vis_utils import plot_model
import keras.backend as K

# Text
from colorama import Fore, Back, Style

# Visualisation libraries
import seaborn as sns
import matplotlib.pyplot as plt
from yellowbrick.features.pcoords import parallel_coordinates
from plotly.offline import init_notebook_mode, iplot 
import plotly.graph_objs as go

# Graphics in retina format 
%config InlineBackend.figure_format = 'retina'

# sns setting
sns.set_context("paper", rc={"font.size":12,"axes.titlesize":14,"axes.labelsize":12})
sns.set_style("whitegrid")

# plt setting
plt.style.use('seaborn-whitegrid')
plt.rcParams['axes.labelsize'] = 14
plt.rcParams['xtick.labelsize'] = 12
plt.rcParams['ytick.labelsize'] = 12
plt.rcParams['text.color'] = 'k'
%matplotlib inline
import warnings
warnings.filterwarnings("ignore")

Daily Weather Data Analysis and Classification using Artificial Neural Network

In this article, we analyze a weather dataset from Kaggle.com. Data description from Kaggle:

  • Sensor measurements from the weather station were captured at one-minute intervals. These measurements were then processed to generate values to describe daily weather. Since this dataset was created to classify
  • low-humidity days vs. non-low-humidity days (that is, days with normal or high humidity), the variables included are weather measurements in the morning, with one measurement, nAMely relatively humidity, in the afternoon. The idea is to use the morning weather values to predict whether the day will be low-humidity or not based on the afternoon measurement of relative humidity.
  • Each row, or sAMple, consists of the following variables:
    • Number: unique number for each row
    • air_pressure_9am: air pressure averaged over a period from 8:55 AM to 9:04 AM (Unit: hectopascals)
    • air_temp_9am: air temperature averaged over a period from 8:55 AM to 9:04 AM (Unit: degrees Fahrenheit)
  • air_wind_direction_9am: wind direction averaged over a period from 8:55AM to 9:04AM (Unit: degrees,
    • with 0 means coming from the North, and increasing clockwise)
  • air_wind_speed_9am: wind speed averaged over a period from 8:55AM to 9:04AM (Unit: miles per hour) max_wind_direction_9am: wind gust direction averaged over a period from 8:55AM to 9:10AM (Unit:
    • degrees, with 0 being North and increasing clockwise)
  • max_wind_speed_9am: wind gust speed averaged over a period from 8:55 AM to 9:04 AM (Unit: miles per hour)
  • rain_accumulation_9am: the AMount of rain accumulated in the 24 hours before 9 AM (Unit: millimeters)
  • rain_duration_9am: the AMount of time rain was recorded in the 24 hours before 9 AM (Unit: seconds)
  • relative_humidity_9am: relative humidity averaged over a period from 8:55 AM to 9:04 AM (Unit: percent)
  • relative_humidity_3pm: relative humidity averaged over a period from 2:55 PM to 3:04 PM (Unit: percent )

The Dataset

Loading the Dataset

In [2]:
Data = pd.read_csv('weatherdata/daily_weather.csv')
Data.drop(columns = ['number'], inplace = True)
Data.head().style.hide_index().set_precision(2)
Out[2]:
air_pressure_9am air_temp_9am avg_wind_direction_9am avg_wind_speed_9am max_wind_direction_9am max_wind_speed_9am rain_accumulation_9am rain_duration_9am relative_humidity_9am relative_humidity_3pm
918.06 74.82 271.10 2.08 295.40 2.86 0.00 0.00 42.42 36.16
917.35 71.40 101.94 2.44 140.47 3.53 0.00 0.00 24.33 19.43
923.04 60.64 51.00 17.07 63.70 22.10 0.00 20.00 8.90 14.46
920.50 70.14 198.83 4.34 211.20 5.19 0.00 0.00 12.19 12.74
921.16 44.29 277.80 1.86 136.50 2.86 8.90 14730.00 92.41 76.74

Features

Columns Description
Air Pressure Air pressure StartFragment in hectopascal (100 pascals) at 9 AM
Air Temperature Air temperature in degrees Fahrenheit at 9 AM
Avg Wind Direction Average wind direction over the minute before the timestamp in degrees (0 starts from the north) at 9 AM
Avg Wind Speed Average wind speed over the minute before the timestamp in meter per seconds (m/s) at 9 AM
Max Wind Direction Highest wind direction in the minute before the timestamp in degrees (0 starts from the north) at 9 AM
Max Wind Speed Highest wind speed in the minute before the timestamp in meter per seconds (m/s) at 9 AM
Min Wind Speed Smallest wind speed in the minute before the timestamp in meter per seconds (m/s) at 9 AM
Rain Accumulation Accumulated rain in millimeters (mm) at 9 AM
Rain Duration Length of time rain in seconds (s) at 9 AM
Relative Humidity (Morning) Relative humidity in percentage in at 9 AM
Relative Humidity (Afternoon) Relative humidity in percentage at 3 PM

For convenience, we would like to modify the feature names.

In [3]:
Data.columns = [x.replace('ty_9am','ty_(Morning)').replace('3pm', '(Afternoon)').replace('_9am', '').replace('_',
                                                      ' ').title().replace('Temp','Temperature') for x in Data.columns.tolist()]
Data.head(5).style.hide_index().set_precision(2)
Out[3]:
Air Pressure Air Temperature Avg Wind Direction Avg Wind Speed Max Wind Direction Max Wind Speed Rain Accumulation Rain Duration Relative Humidity (Morning) Relative Humidity (Afternoon)
918.06 74.82 271.10 2.08 295.40 2.86 0.00 0.00 42.42 36.16
917.35 71.40 101.94 2.44 140.47 3.53 0.00 0.00 24.33 19.43
923.04 60.64 51.00 17.07 63.70 22.10 0.00 20.00 8.90 14.46
920.50 70.14 198.83 4.34 211.20 5.19 0.00 0.00 12.19 12.74
921.16 44.29 277.80 1.86 136.50 2.86 8.90 14730.00 92.41 76.74

Preprocessing

Imputing Missing Values

Note that

In [4]:
def Data_info(Inp, Only_NaN = False):
    Out = Inp.dtypes.to_frame(name='Data Type').sort_values(by=['Data Type'])
    Out = Out.join(Inp.isnull().sum().to_frame(name = 'Number of NaN Values'), how='outer')
    Out['Percentage'] = np.round(100*(Out['Number of NaN Values']/Inp.shape[0]),2)
    if Only_NaN:
        Out = Out.loc[Out['Number of NaN Values']>0]
    return Out
    
Temp = Data_info(Data, Only_NaN = True)
display(Temp)
Temp = Temp.index.tolist()
Data Type Number of NaN Values Percentage
Air Pressure float64 3 0.27
Air Temperature float64 5 0.46
Avg Wind Direction float64 4 0.37
Avg Wind Speed float64 3 0.27
Max Wind Direction float64 3 0.27
Max Wind Speed float64 4 0.37
Rain Accumulation float64 6 0.55
Rain Duration float64 3 0.27
In [5]:
imp = SimpleImputer(missing_values=np.nan, strategy='mean')
Data[Temp] = imp.fit_transform(Data[Temp])
Data_info(Data)
Out[5]:
Data Type Number of NaN Values Percentage
Air Pressure float64 0 0.0
Air Temperature float64 0 0.0
Avg Wind Direction float64 0 0.0
Avg Wind Speed float64 0 0.0
Max Wind Direction float64 0 0.0
Max Wind Speed float64 0 0.0
Rain Accumulation float64 0 0.0
Rain Duration float64 0 0.0
Relative Humidity (Morning) float64 0 0.0
Relative Humidity (Afternoon) float64 0 0.0

Problem Description

Let's set Relative Humidity (Afternoon) as the target variable. This means given the dataset and using the rest of the features, we would like to know whether is humid or not at 3 PM. In doing so, define a Humidity Level (Afternoon) feature as follows:

$$\text{Humidity Level (Afternoon)} = \begin{cases} 0 &\mbox{Very Low} \\ 1 &\mbox{Low} \\ 2 &\mbox{Medium} \\ 3 &\mbox{High} \end{cases}$$
In [6]:
N = 4
Target = 'Humidity Level (Afternoon)'
Data[Target], bins = pd.qcut(Data['Relative Humidity (Afternoon)'], precision =2, retbins= True, q=4, labels=np.arange(0, 4, 1))
df = Data.drop(columns = ['Relative Humidity (Afternoon)'])
Range_dict = dict(list(enumerate(['(%.2f, %.2f]' % (bins[i], bins[i+1]) for i in range(N)])))
del bins

Furthemore, let's look at the variance of our dataset features.

In [7]:
display(df.iloc[:,:-1].var().sort_values(ascending = False).to_frame(name= 'Variance')\
        .style.background_gradient(cmap='OrRd').set_precision(2))
Variance
Rain Duration 2546852.52
Avg Wind Direction 4762.57
Max Wind Direction 4508.55
Relative Humidity (Morning) 648.83
Air Temperature 124.32
Max Wind Speed 31.23
Avg Wind Speed 20.67
Air Pressure 10.11
Rain Accumulation 2.53

Furthermore, we would like to standardize features by removing the mean and scaling to unit variance. In this article, we demonstrated the benefits of scaling data using StandardScaler().

In [8]:
scaler = StandardScaler()
df.iloc[:,:-1] = scaler.fit_transform(df.iloc[:,:-1])

display(df.iloc[:,:-1].var().sort_values(ascending = False).to_frame(name= 'Variance')\
       .style.background_gradient(cmap=sns.light_palette("green", as_cmap=True)).set_precision(2))
Variance
Rain Duration 1.00
Air Temperature 1.00
Avg Wind Speed 1.00
Max Wind Speed 1.00
Max Wind Direction 1.00
Avg Wind Direction 1.00
Relative Humidity (Morning) 1.00
Air Pressure 1.00
Rain Accumulation 1.00
In [9]:
df.describe().style.set_precision(2)
Out[9]:
Air Pressure Air Temperature Avg Wind Direction Avg Wind Speed Max Wind Direction Max Wind Speed Rain Accumulation Rain Duration Relative Humidity (Morning)
count 1095.00 1095.00 1095.00 1095.00 1095.00 1095.00 1095.00 1095.00 1095.00
mean 0.00 -0.00 -0.00 -0.00 0.00 -0.00 0.00 -0.00 0.00
std 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00
min -3.43 -2.53 -1.84 -1.06 -1.79 -1.04 -0.13 -0.18 -1.11
25% -0.73 -0.68 -1.10 -0.72 -1.08 -0.71 -0.13 -0.18 -0.75
50% 0.01 0.07 0.34 -0.36 0.42 -0.37 -0.13 -0.18 -0.43
75% 0.72 0.76 0.71 0.40 0.78 0.33 -0.13 -0.18 0.44
max 3.28 3.05 2.92 3.97 2.43 4.09 14.99 10.91 2.29
In [10]:
def Correlation_Plot (Df,Fig_Size):
    Correlation_Matrix = Df.corr().round(2)
    mask = np.zeros_like(Correlation_Matrix)
    mask[np.triu_indices_from(mask)] = True
    for i in range(len(mask)):
        mask[i,i]=0
    Fig, ax = plt.subplots(figsize=(Fig_Size,Fig_Size))
    sns.heatmap(Correlation_Matrix, ax=ax, mask=mask, annot=True, square=True, 
                cmap =sns.color_palette("Greens", n_colors=10), linewidths = 0.2, vmin=0, vmax=1, cbar_kws={"shrink": .6})

Correlation_Plot (df, 8)

Let's visualize the data using Parallel Coordinates.

In [11]:
X = df.drop(columns = [Target])
y = df[Target]
C = ["#3498db", "#e74c3c", "#34495e", "#2ecc71"]
fig, axes = plt.subplots(nrows=1, ncols=1, figsize = (15, 8))
visualizer = parallel_coordinates(X, y, ax=axes, classes=[Range_dict[i] for i in range(len(Range_dict))],
                          features= X.columns.tolist(),
                          colors = C,
#                           colors = sns.color_palette("bright", N),
                          normalize='standard', sample=0.05, shuffle=True)

del X, y

However, the results of this visualization can be improved if a clustering method is used. For this reason, we K-Means clustering method.

In [12]:
kmeans = KMeans(n_clusters = N)
Temp = df.drop(columns = Target)
model = kmeans.fit(Temp)

Out = pd.DataFrame(model.cluster_centers_, columns = df.iloc[:,1:].columns.tolist())
Out[Target] = np.sort(df[Target].unique().tolist())
Out.style.hide_index()
Out[12]:
Air Temperature Avg Wind Direction Avg Wind Speed Max Wind Direction Max Wind Speed Rain Accumulation Rain Duration Relative Humidity (Morning) Humidity Level (Afternoon)
-0.649172 -1.168453 0.623177 -0.006040 0.689699 -0.048582 -0.038991 0.094987 0
0.858138 -0.216390 -1.282049 1.373276 -1.154235 1.414036 -0.124926 -0.172658 1
-0.057766 0.553788 0.224976 -0.510605 0.158090 -0.512532 -0.119577 -0.161132 2
-0.742911 -1.643634 0.693806 0.234282 0.523196 0.326146 6.573817 6.937917 3
In [13]:
Temp = Out.copy()
Temp['Humidity Level (Afternoon)'] = Temp['Humidity Level (Afternoon)'].map(Range_dict)
fig, ax = plt.subplots(nrows=1, ncols=1, figsize = (16, 8))
_ = pd.plotting.parallel_coordinates(Temp, 'Humidity Level (Afternoon)', lw = 2, color = C, ax = ax)
_ = ax.set_ylim([-3, +10])
_ = ax.set_xticklabels(labels = Temp.columns.tolist(), rotation= 45)
_ = ax.legend(title = 'Humidity Level (Afternoon)', loc = 'upper left', fontsize = 13)

Modeling and Classification

Train and Test sets

In [14]:
X = Data.drop(columns = [Target])
y = pd.get_dummies(Data[Target]).astype(int)

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

pd.DataFrame(data={'Set':['X_train','X_test','y_train','y_test'],
               'Shape':[X_train.shape, X_test.shape, y_train.shape, y_test.shape]}).set_index('Set').T
Out[14]:
Set X_train X_test y_train y_test
Shape (766, 10) (329, 10) (766, 4) (329, 4)

Artifical Neural Network

Here, we implement an artificial neural network (ANN) using Keras.

In [15]:
model = Sequential()
model.add(Dense(12, input_dim= X.shape[1], init='uniform', activation='relu'))
model.add(Dense(10, init='uniform', activation='sigmoid'))
model.add(Dense(4, init='uniform', activation='sigmoid'))
model.add(Dense(y.shape[1], init='uniform', activation='sigmoid'))
# Number of iterations
IT = int(1e3)+1

model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy','mae', 'mse'])

# Train model
history = model.fit(X_train, y_train, nb_epoch= IT, batch_size=50,  verbose=0)
# Predications and Score
y_pred = model.predict(X_test)
score = model.evaluate(X_test, y_test) 
329/329 [==============================] - 0s 73us/step
In [16]:
score = pd.DataFrame(score, index = model.metrics_names).T
history = pd.DataFrame(history.history)
display(score.style.hide_index())
loss accuracy mae mse
0.192360 0.939210 0.201294 0.178553
In [17]:
fig = go.Figure()
fig.add_trace(go.Scatter(x= history.index.values, y= history['loss'].values, line=dict(color='OrangeRed', width= 1.5), 
                         name = 'Loss'))
fig.add_trace(go.Scatter(x= history.index.values, y= history['accuracy'].values, line=dict(color='MidnightBlue', width= 1.5), 
                         name = 'Accuracy'))
fig.add_trace(go.Scatter(x= history.index.values, y= history['mae'].values, line=dict(color='ForestGreen', width= 1.5), 
                         name = 'Mean Absolute Error (MAE)'))
fig.add_trace(go.Scatter(x= history.index.values, y= history['mse'].values, line=dict(color='purple', width= 1.5), 
                         name = 'Mean Squared Error (MSE)'))
fig.update_layout(legend=dict(y=0.5, traceorder='reversed', font_size=12))
fig.update_layout(dragmode='select', plot_bgcolor= 'white', height=600, hovermode='closest')
fig.update_xaxes(showgrid=True, gridwidth=1, gridcolor='Lightgray')
fig.update_yaxes(showgrid=True, gridwidth=1, gridcolor='Lightgray')
fig.update_xaxes(showline=True, linewidth=1, linecolor='Lightgray', mirror=True)
fig.update_yaxes(showline=True, linewidth=1, linecolor='Lightgray', mirror=True)
fig['layout']['xaxis'].update(range=[0, history.index.values.max()])
fig['layout']['yaxis'].update(range=[0, 1.6])
fig.show()

Finally, a summary and a glimpse of the model.

In [18]:
model.summary()
plot_model(model, show_shapes=True, show_layer_names=True, expand_nested = True)
Model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_1 (Dense)              (None, 12)                132       
_________________________________________________________________
dense_2 (Dense)              (None, 10)                130       
_________________________________________________________________
dense_3 (Dense)              (None, 4)                 44        
_________________________________________________________________
dense_4 (Dense)              (None, 4)                 20        
=================================================================
Total params: 326
Trainable params: 326
Non-trainable params: 0
_________________________________________________________________
Out[18]: